package net.anzhuo.controller.db.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import net.anzhuo.common.entity.PaginationVO;
import net.anzhuo.common.utils.RedisUtil;
import net.anzhuo.entity.GsZhuanli;
import net.anzhuo.entity.db.zhuanli.Paging;
import net.anzhuo.entity.db.zhuanli.ResponseResult;
import net.anzhuo.entity.db.zhuanli.Result;
import net.anzhuo.service.GsZhuanliService;

@RestController
@Slf4j
public class ZhuanliController {

	@Autowired
	private GsZhuanliService gsZhuanliService;

	
	@RequestMapping("/report/zhuanli")
	public ResponseResult getZhuanli(String searchKey, String pageSize) {
		log.info("===>getZhuanli_searchKey="+searchKey+", pageSize="+pageSize);
		long startTime=System.currentTimeMillis();
		if (StringUtils.isBlank(pageSize)) {
			pageSize = "50";
		}
		
		ResponseResult rr = null;
		try {
			rr = (ResponseResult)RedisUtil.get("report:zhuanli:"+searchKey);
			if (rr != null) {
				 long endTime=System.currentTimeMillis();
				 float excTime=(float)(endTime-startTime)/1000;
				 log.info(" report:zhuanli 执行时间："+excTime+"s");
				return rr;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		GsZhuanli gsZhuanliQuery = new GsZhuanli();
	
		gsZhuanliQuery.setCoMd5(searchKey);
		gsZhuanliQuery.setPageSize(Integer.parseInt(pageSize));
		PaginationVO<GsZhuanli> zhuanliList = gsZhuanliService.getListWithPage(gsZhuanliQuery);
		
		ResponseResult responseResult = new ResponseResult();
		responseResult.setStatus("200");
		responseResult.setMessage("查询成功");
		
		Paging paging = new Paging();
		paging.setPageIndex(1);
		paging.setPageSize(Integer.parseInt(pageSize));
		paging.setTotalRecords(zhuanliList.getTotalCount());
		responseResult.setPaging(paging);
		
		List<Result> resultList = new ArrayList<>();
		Result result = null;
		if (zhuanliList.getList()!=null && zhuanliList.getList().size()>0) {
			for (GsZhuanli z : zhuanliList.getList()) {
				result = new Result();
				result.setPublicationDate(z.getApplyPubTime());
				result.setTitle(z.getPatentName());
				result.setApplicationNumber(z.getApplyNumber());
				result.setPublicationNumber(z.getApplyPubNumber());
				
				result.setAddress(z.getAddress());
				result.setAgency(z.getAgency());
				result.setAgent(z.getAgent());
				result.setDescription(z.getDescription());
				result.setImgSrc(z.getImgsrc());
				result.setInventor(z.getInventor());
				result.setApplicationTime(z.getApplicationTime());
				result.setApplicantName(z.getApplicantName());
				result.setInventType(z.getPatentType());
							
				resultList.add(result);
			}
		}
		responseResult.setResult(resultList);
		
		try {
			RedisUtil.set("report:zhuanli:"+searchKey, responseResult);
		} catch (Exception e) {
			e.printStackTrace();
		}
		 long endTime=System.currentTimeMillis();
		 float excTime=(float)(endTime-startTime)/1000;
		 log.info(" report:zhuanli 执行时间："+excTime+"s");
		return responseResult;
	}
}
